#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <winsock2.h>  // 添加此行以包含 struct in_addr 的定义
#include "aodv_routing_table.h"

#define ROUTING_TABLE_MAX_ENTRIES 100

// 获取当前时间（秒）
static uint32_t get_current_time() {
    return (uint32_t)time(NULL);
}

// 初始化路由表
void routing_table_init(aodv_routing_table *table) {
    if (table == NULL) return;
    table->count = 0;
    memset(table->entries, 0, sizeof(table->entries));
}

// 添加或更新路由条目
int routing_table_add_or_update(aodv_routing_table *table, const routing_table_entry *entry) {
    if (table == NULL || entry == NULL) return -1;
    if (table->count >= ROUTING_TABLE_MAX_ENTRIES) return -1;

    // 查找是否已存在该目的地址的条目
    for (int i = 0; i < table->count; i++) {
        if (table->entries[i].dest_addr == entry->dest_addr) {
            // 更新现有条目
            table->entries[i] = *entry;
            printf("Updated route to %s\n", inet_ntoa(*(struct in_addr*)&entry->dest_addr));
            return 0;
        }
    }

    // 添加新条目
    table->entries[table->count] = *entry;
    table->count++;
    printf("Added new route to %s\n", inet_ntoa(*(struct in_addr*)&entry->dest_addr));
    return 0;
}

// 查找路由条目
routing_table_entry* routing_table_find(aodv_routing_table *table, uint32_t dest_addr) {
    if (table == NULL) return NULL;

    for (int i = 0; i < table->count; i++) {
        if (table->entries[i].dest_addr == dest_addr) {
            return &table->entries[i];
        }
    }
    return NULL;
}

// 删除路由条目
int routing_table_delete(aodv_routing_table *table, uint32_t dest_addr) {
    if (table == NULL) return -1;

    for (int i = 0; i < table->count; i++) {
        if (table->entries[i].dest_addr == dest_addr) {
            // 将最后一个条目移动到当前位置（如果当前不是最后一个）
            if (i < table->count - 1) {
                table->entries[i] = table->entries[table->count - 1];
            }
            table->count--;
            printf("Deleted route to %s\n", inet_ntoa(*(struct in_addr*)&dest_addr));
            return 0;
        }
    }
    return -1; // 未找到条目
}

// 检查路由是否有效（未过期）
int routing_table_is_valid(aodv_routing_table *table, uint32_t dest_addr) {
    if (table == NULL) return 0;

    routing_table_entry *entry = routing_table_find(table, dest_addr);
    if (entry == NULL) return 0;

    uint32_t current_time = get_current_time();
    return (entry->lifetime > current_time) ? 1 : 0;
}

// 打印路由表
void routing_table_print(const aodv_routing_table *table) {
    if (table == NULL) return;

    printf("\n=== AODV Routing Table ===\n");
    printf("Total entries: %d\n", table->count);
    printf("%-20s %-10s %-20s %-10s %-10s\n", 
           "Destination", "Seq Num", "Next Hop", "Hop Count", "Lifetime");
    printf("---------------------------------------------------------------\n");

    for (int i = 0; i < table->count; i++) {
        const routing_table_entry *entry = &table->entries[i];  // 使用 const 指针
        printf("%-20s %-10u %-20s %-10u %-10u\n",
               inet_ntoa(*(struct in_addr*)&entry->dest_addr),
               entry->dest_seq_num,
               inet_ntoa(*(struct in_addr*)&entry->next_hop_addr),
               entry->hop_count,
               entry->lifetime);
    }
    printf("========================\n\n");
}